<style lang="less">
@import './main.less';
</style>
<template>
    <div class="main" :class="{'main-hide-text': shrink}">
        <div class="sidebar-menu-con" :style="{width: shrink?'60px':'160px', overflow: shrink ? 'visible' : 'auto'}">
            <shrinkable-menu
                :shrink="shrink"
                @on-change="handleSubmenuChange"
                :theme="menuTheme"
                :before-push="beforePush"
                :open-names="openedSubmenuArr"
                :menu-list="menuList">
                <div slot="top" class="logo-con">
                    <img v-show="!shrink"  src="/static/img/logo.png" key="max-logo" />
                    <img v-show="shrink" src="/static/img/logo-min.png" key="min-logo" />
                </div>
            </shrinkable-menu>
        </div>
        <div class="main-header-con" :style="{paddingLeft: shrink?'60px':'160px'}">
            <div class="main-header">
                <div class="navicon-con">
                    <Button :style="{transform: 'rotateZ(' + (this.shrink ? '-90' : '0') + 'deg)'}" type="text" @click="toggleClick">
                        <Icon type="navicon" size="32" style="color: #fff;"></Icon>
                    </Button>
                </div>

                <div class="header-avator-con">
                    <!-- <full-screen v-model="isFullScreen" @on-change="fullscreenChange"></full-screen>
                    <lock-screen></lock-screen> -->
                    <div class="user-dropdown-menu-con">
                        <Row type="flex" justify="end" align="middle" class="user-dropdown-innercon">
                            <Dropdown transfer trigger="hover" @on-click="handleClickUserDropdown">
                                <a href="javascript:void(0)">
                                    <span class="main-user-name" v-if="!roleId">
                                        登录信息失效
                                    </span>
                                    <span class="main-user-name" v-if="roleId">
                                        <template v-if="roleId == 100">
                                            {{roleName}} - {{clientName}} - {{telephone}}
                                        </template>
                                        <template v-else>
                                            {{clientName}} - {{roleName}} - {{userName}} - {{telephone}}
                                        </template>
                                    </span>
                                    <Icon class="main-user-arrow" type="arrow-down-b"></Icon>
                                </a>
                                <DropdownMenu slot="list">
                                    <DropdownItem name="editPwd">修改密码</DropdownItem>
                                    <!-- <DropdownItem name="helpSpace">帮助中心</DropdownItem> -->
                                    <DropdownItem name="loginout" divided>安全退出</DropdownItem>
                                </DropdownMenu>
                            </Dropdown>
                            <!-- <Avatar :src="avatorPath" style="background: #619fe7;margin-left: 10px;"></Avatar> -->
                        </Row>
                    </div>

                    <div class="top-icon-wrap" v-if="isNotMigrateClient">

                        <Poptip class="icon-in-top" transfer trigger="hover">
                            <c-icon
                                name="qrcode"
                                align="middle"
                                className="icon-large"></c-icon>

                            <div slot="content" class="header-code-wrap clear-fix">
                                <div class="header-code">
                                    <div class="code-img img-first">
                                        <img :src="qrCode" alt="APP二维码">
                                    </div>
                                    <!-- <div>云丁助手</div> -->
                                </div>
                            </div>
                        </Poptip>

                        <div class="help-space icon-in-top" @click="showHelpSpace">
                            <Tooltip content="帮助中心">
                                <c-icon
                                    style="margin-top:-3px"
                                    name="message-center"
                                    align="middle"
                                    className="icon-large"></c-icon>
                            </Tooltip>
                        </div>

                        <Tooltip content="消息中心">
                            <c-icon
                            name="help-center"
                            align="middle"
                            @click="showMessage"
                            className="icon-large"></c-icon>
                        </Tooltip>
                    </div>
                    <div class="header-tel">
                        <span style="font-weight:normal">客服热线：</span>4000-101-110
                    </div>
                </div>
            </div>
        </div>
        <div class="single-page-con" :style="{left: shrink?'60px':'160px'}">
            <div class="single-page" :class="{'no-margin':$route.name=='home_index'}">
                <!-- <keep-alive :include="cachePage">
                    <router-view></router-view>
                </keep-alive> -->
                <router-view></router-view>
            </div>
        </div>
    </div>
</template>
<script>
import shrinkableMenu from './main-components/shrinkable-menu/shrinkable-menu.vue'
import util from '@/libs/util.js'
import { http, api } from '@/libs/http'
export default {
  components: {
    shrinkableMenu
  },
  data () {
    return {
      qrCode: '/static/img/qrcode-ydzs.png',
      // appAndroid: '/static/img/app-android.png',
      shrink: false,
      // userName: '',
      isFullScreen: false,
      openedSubmenuArr: this.$store.state.app.openedSubmenuArr
    }
  },
  computed: {
    menuList () {
      return this.$store.state.app.menuList
    },
    isNotMigrateClient () { // 不是2.0分账升级用户
      return !this.$store.state.user.isMigrateClient
    },
    pageTagsList () {
      return this.$store.state.app.pageOpenedList // 打开的页面的页面对象
    },
    currentPath () {
      return this.$store.state.app.currentPath // 当前面包屑数组
    },
    clientName () {
      const client = this.$store.state.user.client
      return client ? client.name : ''
    },
    roleId () {
      return this.$store.state.user.roleId || ''
    },
    roleName () {
      return this.$store.state.user.roleName || ''
    },
    userName () {
      return this.$store.state.user.userName || ''
    },
    telephone () {
      return this.$store.state.user.telephone || ''
    },
    client () {
      return this.$store.state.user.client || ''
    },
    cachePage () {
      return this.$store.state.app.cachePage
    },
    lang () {
      return this.$store.state.app.lang
    },
    menuTheme () {
      return this.$store.state.app.menuTheme
    },
    mesCount () {
      return this.$store.state.app.messageCount
    }
  },
  methods: {
    checkUserInfo () {
      // 防止页面刷新丢失 vuex 里面的user信息
      const roleId = this.$store.state.user.roleId
      if (!roleId) {
        http.request(this, api.user_info, null, null, (res, err) => {
          if (err || !res) {
            return
          }
          this.$store.commit('updateUserInfo', res)
          this.$store.commit('setPermissionList', res)
        })
      }
    },
    toggleClick () {
      this.shrink = !this.shrink
    },
    handleClickUserDropdown (name) {
      if (name === 'editPwd') {
        window.open('https://passport.dding.net/userinfo?from=2')
      }
      if (name === 'helpSpace') {
        util.openNewPage(this, 'helpspace_index')
        this.$router.push({
          name: 'helpspace_index'
        })
      } else if (name === 'loginout') {
        http.logout()
      }
    },
    checkTag (name) {
      let openpageHasTag = this.pageTagsList.some(item => {
        if (item.name === name) {
          return true
        }
      })
      if (!openpageHasTag) {
        //  解决关闭当前标签后再点击回退按钮会退到当前页时没有标签的问题
        util.openNewPage(this, name, this.$route.params || {}, this.$route.query || {})
      }
    },
    handleSubmenuChange (val) {
      // console.log(val)
    },
    beforePush (name) {
      // if (name === 'accesstest_index') {
      //     return false;
      // } else {
      //     return true;
      // }
      return true
    },
    fullscreenChange (isFullScreen) {
      // console.log(isFullScreen);
    },
    showMessage () {
      util.openNewPage(this, 'message_index')
      this.$router.push({
        name: 'message_index'
      })
    },
    showHelpSpace () {
      util.openNewPage(this, 'helpspace-wrap')
      this.$router.push({
        name: 'helpspace-wrap'
      })
    }
  },

  created () {
    // 检查是否有用户信息
    this.checkUserInfo()
  }
}
</script>
